package com.simplesoftwares.client.library.appstructure;

import com.google.gwt.view.client.NoSelectionModel;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;

/**
 * This class applies selection model on Super Table.
 * Since it also implements {@link SelectionChangeEvent.Handler} so we need not apply
 * Handler from outside.
 * On click of table row any form get filled , The functionality comes from this class.
 * @author Kamala
 * @param <T>
 */

public class TableSelection<T> extends NoSelectionModel<T> implements  SelectionChangeEvent.Handler
{
  protected UiScreen<T> mv;
	
	/**
	 * Zero args constructor
	 */
   public TableSelection()
	{
		super();
	}
   /**
    * 
    * @param mv Ui screen which will get filled on row click.
    * @param keyProvider Key Provider object
    */
	
	public TableSelection(UiScreen<T> mv,ProvidesKey<T>keyProvider)
	{
		super(keyProvider);
		this.mv=mv;
		addSelectionChangeHandler(this);
	}
	
	/**
	 * Crucial method provides functionality of view filling on row click.
	 * To Do: Need to exactly decide weather click is on pop up table or in normal table.
	 * @author Kamala
	 * 
	 */
	@Override
	public void onSelectionChange(SelectionChangeEvent event) 
	{	
		// Get the clicked object
		T obj=TableSelection.this.getLastSelectedObject();
		//Type cast in super model(if the obj is not type of supermodel functionality will break)
		
        SuperModel model=(SuperModel) obj;
        if(mv==null)
        	return;
        mv.getPresenter().setModel(model);
        //Clears the screen
		mv.clear();
		//updates the view 
		mv.updateView(obj);
		mv.setToViewState();
		
		//If row is click is in pop up we need to hide it
		if(mv.getSearchpopupscreen()!=null)
		{
			System.out.println("MMMMMMMMMMMMMMMMMMMMMMMM"+mv.getSearchpopupscreen());
			mv.getSearchpopupscreen().hidePopUp();	 
			
		}
	}
   
}
